﻿using System.Collections.Generic;
using System.Management;
using System.Net.NetworkInformation;
using System.Text;

namespace CommonLibrary.Utils
{
    public class HardwareInfo
    {
        private static string macAddress;
        private static string cpuSerialNumber;
        private static string boardSerialNumber;
        private static string hardDiskNumber;

        /// <summary>
        /// 获取网卡mac地址
        /// </summary>
        /// <returns></returns>
        public static string GetMacInfo()
        {
            if (!string.IsNullOrEmpty(macAddress))
                return macAddress;

            NetInfo netInfo = new NetInfo();

            // 本地计算机网络连接信息
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();

            netInfo.HostName = computerProperties.HostName;
            netInfo.DomainName = computerProperties.DomainName;

            // 获取本机所有网络连接
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            if (nics == null || nics.Length < 1)
                return string.Empty;

            foreach (NetworkInterface adapter in nics)
            {
                NetInfo.MacInfo macInfo = new NetInfo.MacInfo();
                macInfo.Name = adapter.Name;
                macInfo.Description = adapter.Description;
                macInfo.NetworkInterfaceType = adapter.NetworkInterfaceType;
                PhysicalAddress address = adapter.GetPhysicalAddress();
                byte[] bytes = address.GetAddressBytes();
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    builder.Append(bytes[i].ToString("X2"));
                    if (i != bytes.Length - 1)
                    {
                        builder.Append("-");
                    }
                }
                macInfo.Mac = builder.ToString();
                netInfo.MacInfos.Add(macInfo);
            }

            return macAddress;
        }

        /// <summary>
        /// 获取Cpu序列号
        /// </summary>
        /// <returns></returns>
        public static string GetCpuSerialNumber()
        {
            if (string.IsNullOrEmpty(cpuSerialNumber))
            {
                try
                {
                    using ManagementClass mc = new ManagementClass("Win32_Processor");
                    using ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        cpuSerialNumber = mo.Properties["ProcessorId"].Value?.ToString();
                        break;
                    }
                }
                catch { }
            }
            return cpuSerialNumber;
        }

        /// <summary>
        /// 获取主板序列号
        /// </summary>
        /// <returns></returns>
        public static string GetBoardSerialNumber()
        {
            if (string.IsNullOrEmpty(boardSerialNumber))
            {
                try
                {
                    using ManagementClass mc = new ManagementClass("Win32_BaseBoard");
                    using ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        boardSerialNumber = mo.Properties["SerialNumber"].Value?.ToString();
                        break;
                    }
                }
                catch { }
            }
            return boardSerialNumber;
        }

        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns></returns>
        public static string GetHardDiskSerialNumber()
        {
            if (string.IsNullOrEmpty(hardDiskNumber))
            {
                try
                {
                    using ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
                    using ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        hardDiskNumber = mo.Properties["SerialNumber"].Value?.ToString();
                        break;
                    }
                }
                catch { }
            }
            return hardDiskNumber;
        }
    }

    public class NetInfo
    {
        /// <summary>
        /// 本机电脑名
        /// </summary>
        public string HostName { get; set; }

        /// <summary>
        /// 域名
        /// </summary>
        public string DomainName { get; set; }

        public List<MacInfo> MacInfos { get; set; } = new List<MacInfo>();

        public class MacInfo
        {
            public string Name { get; set; }
            public string Description { get; set; }
            public NetworkInterfaceType NetworkInterfaceType { get; set; }
            public string Mac { get; set; }
        }
    }
}